<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属乡镇" prop="townShipId">
        <el-select v-model="queryParams.townShipId" clearable filterable placeholder="请选择"
          @change="townChange(queryParams.townShipId)">
          <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商家名" prop="venueCpId" v-if="venueList.length != 0">
        <el-select v-model="queryParams.venueCpId" clearable filterable placeholder="请选择">
          <el-option v-for="item in venueList" :key="item.id" :label="item.venueName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="tj">
      未核销统计:{{ writeCount }}张
    </div>
    <!-- 页面操作按钮 -->
    <el-row :gutter="10">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:label:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:label:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:label:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleWriteoff"
          v-hasPermi="['system:label:remove']">一键核销</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:label:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- table表格 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="活动名" align="center" prop="activityTitle" />
      <el-table-column label="商家名" align="center" prop="venueName" />
      <el-table-column label="商家核销人名" align="center" prop="venueCpUname" />
      <el-table-column label="商家核销时间" align="center" prop="venueCpTime" />
      <el-table-column label="公司是否已经核销" align="center" prop="adminCpState">
        <template slot-scope="scope">
          {{ !scope.row.adminCpState ? '未核销' : '已核销' }}
        </template>
      </el-table-column>
      <el-table-column label="公司核销时间" align="center" prop=" adminCpTime">
        <template slot-scope="scope">
          {{ !scope.row.adminCpTime ? '暂无核销时间' : scope.row.adminCpTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-reading" @click="handleWriteoff(scope.row)"
            v-hasPermi="['view:accommodation_introduction:edit']">核销</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['view:accommodation_introduction:edit']">修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['view:accommodation_introduction:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改标签对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="所属活动" prop="activityId">
          <el-select v-model="form.activityId" clearable filterable placeholder="请选择">
            <el-option v-for="item in activityList" :key="item.id" :label="item.title" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属乡镇" prop="townShipId">
          <el-select v-model="form.townShipId" clearable filterable placeholder="请选择"
            @change="townChange(form.townShipId)">
            <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属商家" prop="venueId">
          <el-select v-model="form.venueId" clearable filterable multiple placeholder="请选择">
            <el-option v-for="item in venueList" :key="item.id" :label="item.venueName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="到期时间" prop="expirationTime">
          <el-date-picker v-model="form.expirationTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { writeOff_list, writeOff_company, writeOff_delete, venue_list } from "@/api/view/writeOff";
import { town_list } from "../../../api/view/activity_information"
import ImageUpload from "@/components/UploadImage";
import Editor from "@/components/Editor";
import zxMap from "@/components/map/index";
export default {
  name: "Label",
  components: {
    ImageUpload,
    Editor,
    zxMap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        venueCpId: null,
        townShipId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入", trigger: "blur" }],
        remark: [{ required: true, message: "请输入", trigger: "blur" }],
        townShipId: [{ required: true, message: "请选择", trigger: "change" }],
        activityId: [{ required: true, message: "请选择", trigger: "change" }],
        expirationTime: [{ required: true, message: "请选择", trigger: "change" }],
        venueId: [{ required: true, message: "请选择", trigger: "change" }],

      },
      townList: [], //乡镇列表
      activityList: [], //活动列表
      venueList: [],   //商家列表
      writeCount: 0,    //核销记录总数
    };
  },
  created() {
    this.getList();
    this.getTownList();
  },
  mounted() {

  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        activityId: null,
        expirationTime: null,
        remark: null,
        townShipId: null,
        venueId: 0,
      };
      this.resetForm("form");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      writeOff_list(this.queryParams).then((response) => {
        this.list = response.rows;
        if (response.rows.length != 0) {
          this.writeCount = response.rows[0].count;
        }else{
          this.writeCount = 0;
        };
        this.total = response.total;
        this.loading = false;
      });
    },
    /* 获取乡镇列表 */
    getTownList() {
      town_list().then(res => {
        console.log(res.data, '乡镇列表');
        this.townList = res.data;
      });
    },
    /* 获取商家列表 */
    getVenueList(id) {
      venue_list({ townShipId: id }).then(res => {
        this.venueList = res.data;
        console.log(res, '商家列表');
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /* 核销按钮事件 */
    handleWriteoff(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认核销商家为"' + row.venueName +'"标题为"'+row.title+'"的券码数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return writeOff_company(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("核销成功");
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加券码";
    },
    townChange(id) {
      this.getVenueList(id);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.townChange(row.townShipId);
      row.venueId = !row.venueId ? [] : row.venueId.split(",").map(Number);
      console.log(row, 'row');
      this.form = row;
      this.open = true;
      this.title = "修改卷码";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.venueId = data.venueId.join(",");
          if (data.id != null) {
            couponCode_update(data).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            couponCode_add(data).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商家为"' + row.venueName +'"标题为"'+row.title+'"的券码数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return writeOff_delete(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm("是否确认导出所有标签数据项?", "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(function () {
    //       return exportLabel(queryParams);
    //     })
    //     .then((response) => {
    //       this.download(response.msg);
    //     });
    // },
    //tabel表单商品switch change事件
    commodityOpt(val) {
      commodity_update(val).then(res => {
        this.msgSuccess(res.msg);
        this.getList();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.tj {
  font-size: 25px;
  // padding: 10px 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  position: absolute;
  top: 10px;
  right: 20px;
}
</style>
